package com.sun.module_base.app

import android.app.Application
import android.content.ContentProvider
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import androidx.lifecycle.ProcessLifecycleOwner
import com.sun.module_base.ext.lifecycle.KtxAppLifeObserver
import com.sun.module_base.ext.lifecycle.KtxLifeCycleCallBack
import com.sun.module_base.network.manager.NetworkStateManager

/**
 *@author:sunc
 *@time :2025/7/21 9:34
 *@desc: 获取Application
 */
val appContext: Application by lazy { Ktx.app }

class Ktx : ContentProvider() {


    override fun onCreate(): Boolean {
        val application = context!!.applicationContext as Application
        install(application)
        return true
    }

    private fun install(application: Application) {
        app = application
        NetworkStateManager.getInstance(application).register()
        if (watchActivityLife) application.registerActivityLifecycleCallbacks(KtxLifeCycleCallBack())
        if (watchAppLife) ProcessLifecycleOwner.get().lifecycle.addObserver(KtxAppLifeObserver())
    }

    override fun delete(
        uri: Uri,
        selection: String?,
        selectionArgs: Array<out String?>?
    ): Int = 0

    override fun getType(uri: Uri): String? = null

    override fun insert(
        uri: Uri,
        values: ContentValues?
    ): Uri? = null


    override fun query(
        uri: Uri,
        projection: Array<out String?>?,
        selection: String?,
        selectionArgs: Array<out String?>?,
        sortOrder: String?
    ): Cursor? = null

    override fun update(
        uri: Uri,
        values: ContentValues?,
        selection: String?,
        selectionArgs: Array<out String?>?
    ): Int = 0

    companion object {
        lateinit var app: Application
        var watchActivityLife = true
        val watchAppLife = true
    }

}